import { JsonAsset } from "cc";
import { BaseEventName, LoadingProcess } from "../../framework/config/Appcfg";
import { EventMgr } from "../../framework/manager/EventMgr";
import { LogMgr } from "../../framework/manager/LogMgr";
import { ResourceMgr } from "../../framework/manager/ResourceMgr";
import { GameData } from "../excel/GameData";
import { PlayerMgr } from "./PlayerMgr";
export class DataMgr {
    private static instance: DataMgr;
    private constructor() { }
    public static getInstance(): DataMgr {
        if (this.instance == null) {
            this.instance = new DataMgr();
        }
        return this.instance;
    }
    private data: GameData[];

    private _currConfigData = null
    get currConfigData(){
        if(this._currConfigData){
            return this._currConfigData
        }
        // if(window['wx']){
        //     let res = window['wx'].getExptInfoSync(['expt_difficulty']) as any
        //     if(res.expt_difficulty == '0'){
        //         this._currConfigData = "GameJsonCfgA"
        //         console.warn("当前配置版本为A版本")
        //     }
        //     else if(res.expt_difficulty == '1'){
        //         this._currConfigData = "GameJsonCfgB"
                
        //         console.warn("当前配置版本为B版本")
        //     }else if(!res.expt_difficulty){
        //         this._currConfigData = "GameJsonCfgA"
                
        //     }else{
        //         console.warn("后台字段 expt_difficulty 未知，请检查配置是否正确：",res)
        //         this._currConfigData = "GameJsonCfgA"
               
        //     }
        //     console.warn("当前配置版本为",this._currConfigData,"res:",res)
        // }else{
        //     this._currConfigData = "GameJsonCfgA"
          
        // }
        this._currConfigData = "GameJsonCfg"
        PlayerMgr.instance.ConifgName =""// this._currConfigData;
        return this._currConfigData
    }

    /**加载 */
    public preload() {
        //加载配置json
        ResourceMgr.getInstance().loadRes("configs", this.currConfigData, JsonAsset, (res: JsonAsset) => {
            this.data = res.json as GameData[];
            res.decRef();
            EventMgr.getInstance().emit(BaseEventName.Loading, LoadingProcess.ExcelCfg)
            LogMgr.getInstance().debug("加载用户数据")
        })
        
        // ResourceMgr.getInstance().preload("gameTextrue", ['1','2','3','4','5','6','7'])
    }
    //不同版本的配置表名
    getLevelCof(level: number,gameType:number=0) {
        let cfg: GameData;
        let currLevelData = this.data[0] as GameData[]
        for(let k in currLevelData){
            if(currLevelData[k].level == level /**&& currLevelData[k].gameType === gameType**/){
                cfg = currLevelData[k]
                break;
            }
        }
        if (!cfg){
             //随机查找数据中gametype为1的配置
             let arr = currLevelData
            cfg = arr[Math.floor(Math.random() * arr.length)]
        }
        console.log("cfg",cfg,currLevelData)
        return cfg
    }
    /**GM */
    getGmData() {
        return this.data;
    }

}